package com.jinlejia.bbs.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsAdapterContributeBinding;
import com.jinlejia.bbs.ui.mine.others.BBSOtherUserMainActivity;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;

import java.util.ArrayList;

/**
 * 贡献月榜列表适配器
 * @author 王进 on 2019/3/4.
 */

public class ContributeAdapter extends BaseLoadAdapter<ArrayList<BBSUserBean>>{
    private final int TYPE_ITEM = 2;
    public ContributeAdapter(Context context){
        super(context);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case TYPE_ITEM:
                viewHolder = new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_contribute, parent, false));
                break;
            default:
                throw new IllegalArgumentException();
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder){
            bindItemViewHolder((ItemViewHolder) holder,position);
        }
    }

    //region Item
    private class ItemViewHolder extends RecyclerView.ViewHolder{

        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
    private String [] color = {"#C8A100","#7B8FAD","#BF7237","#333333"};
    private int [] headerSrc = {R.mipmap.bbs_contribute_top1,R.mipmap.bbs_contribute_top2,R.mipmap.bbs_contribute_top3};
    private int [] headerImgSrc = {R.mipmap.bbs_contribute_img_top1,R.mipmap.bbs_contribute_img_top2,R.mipmap.bbs_contribute_img_top3};
    private void bindItemViewHolder(ItemViewHolder holder,int position){
        BbsAdapterContributeBinding binding = DataBindingUtil.getBinding(holder.itemView);
        binding.sortNumTextView.setText(String.valueOf(position+1));

        if (position>=headerSrc.length){
            binding.levelImageView.setVisibility(View.INVISIBLE);
            Drawable round = new ColorDrawable(Color.TRANSPARENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                binding.headerImgLayout.setBackground(round);
            }else {
                binding.headerImgLayout.setBackgroundDrawable(round);
            }
        }else {
            binding.levelImageView.setVisibility(View.VISIBLE);
            binding.levelImageView.setImageResource(headerSrc[position]);
            binding.headerImgLayout.setBackgroundResource(headerImgSrc[position]);
        }
        if (position>=color.length){
            binding.sortNumTextView.setTextColor(Color.parseColor(color[color.length-1]));
        }else {
            binding.sortNumTextView.setTextColor(Color.parseColor(color[position]));
        }
        final BBSUserBean bean = mData.get(position);
        binding.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, BBSOtherUserMainActivity.class);
                intent.putExtra("toUser",false);
                intent.putExtra("pin",bean.getUserPin());
                mContext.startActivity(intent);
            }
        });
        binding.setBean(bean);
    }
    //endregion

    @Override
    public int getItemCount() {
        if (mData==null)return 1;
        if (mData.size()==0){
            return 1;
        }
        return mData.size();
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return TYPE_ITEM;
    }
}
